from django.core.management.base import BaseCommand
from apps.user.models import SysUser

class Command(BaseCommand):
    help = 'Import users data from dictionary into SysUser table'

    def handle(self, *args, **kwargs):
        # 直接定义字典数据
        users_data = [
            {
                'username': 'user1',
                'password': 'password1',
                'email': 'user1@example.com',
                'phonenumber': '13800138000',
                'status': 0,
                'remark': 'First user'
            },
            {
                'username': 'user2',
                'password': 'password2',
                'email': 'user2@example.com',
                'phonenumber': '13800138001',
                'status': 1,
                'remark': 'Second user'
            },
            {
                'username': 'user3',
                'password': 'password3',
                'email': 'user3@example.com',
                'phonenumber': '13800138002',
                'status': 0,
                'remark': 'Third user'
            },
            # 可以继续添加更多的用户数据
        ]

        # 创建一个 SysUser 对象列表
        user_objects = []
        for user_data in users_data:
            user = SysUser(
                username=user_data['username'],
                email=user_data['email'],
                phonenumber=user_data['phonenumber'],
                status=user_data['status'],
                remark=user_data['remark']
            )
            # 加密密码
            user.set_password(user_data['password'])
            user_objects.append(user)

        # 批量插入到数据库
        SysUser.objects.bulk_create(user_objects)

        self.stdout.write(self.style.SUCCESS(f'Successfully imported {len(user_objects)} users'))